package com.zzuli.yxy.task;

import com.zzuli.yxy.entity.Song;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.zzuli.yxy.service.SongService;
import com.zzuli.yxy.task.springDataPipeline.SongLoadPipeline;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;

import java.util.List;

/**
 * 目标地址：http://music.163.com/song/media/outer/url?id=436514312.mp3
 * 下载歌曲到自己的云端
 * @auther pony
 * @create 2023-04-05 18:51
 */
@Component
public class SongLoadTask implements PageProcessor {

    @Autowired
    private SongService songService;

    private String basicUrl = "http://music.163.com/song/media/outer/url?id=436514312.mp3";

    @Override
    public void process(Page page) {
        //1、根据路径，获取歌曲id
        String url = page.getUrl().toString();
        System.out.println("url:"+url);
        //截取获得 songId
        String songId = url.substring(url.lastIndexOf("=") + 1, url.lastIndexOf("."));
        System.out.println("songId:"+songId);

        //2、将页面数据转换为二进制数据存入byte数组
        byte[] songBytes = page.getBytes();

        //3、保存数据给 Pipeline 进行数据库保存
        page.putField("songId", songId);
        page.putField("songBytes", songBytes);
    }

    private Site site = Site.me();
    @Override
    public Site getSite(){
        return site;
    }

    @Autowired
    private SongLoadPipeline songLoadPipeline;
//    @Scheduled(initialDelay = 1 * 1000, fixedRate=1000*60*60*24*7)
    public void process() {
        //1、查询数据库，获取所有歌曲
        List<Song> songList = songService.list();
        //2、遍历 songList，得到每首歌的 id
        for (Song song : songList) {
            // 3、通过id，拼接要爬虫地址
            Integer songId = song.getSongId();
            String url = "http://music.163.com/song/media/outer/url?id=" + songId + ".mp3";
            // 4、配置请求信息
            Request request = new Request(url);
            // 请求方法
            request.setMethod("GET");
            // 浏览器基本信息，用于反爬
            request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36");

            Spider.create(new SongLoadTask())
                    .addRequest(request)
                    // 使用布隆过滤器进行去重， 10000 大概要放的数据，数据越大越慢
                    // 使用前先引一个新版本的依赖
                    .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(10000)))
                    .addPipeline(this.songLoadPipeline)
                    .thread(10)
                    .run();
        }
    }
}
